import { NgModule, SkipSelf, Optional } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { MatIconRegistry, MAT_DATE_LOCALE, DateAdapter, MAT_DATE_FORMATS, MatDatepickerIntl } from '@angular/material';
import { MomentDateAdapter } from '@angular/material-moment-adapter';
import { DomSanitizer } from '@angular/platform-browser';
import {SharedModule} from "../shared/shared.module"
import { AppRoutingModule } from '../app-routing.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
import { SidebarComponent } from './sidebar/sidebar.component';
import {loadSvgResources} from "../utils/svg.util";
import { MD_FNS_DATE_FORMATS } from '../shared/adapters/date-formats';
import { DatepickerI18n } from '../shared/adapters/datepicker-i18n';

@NgModule({
  declarations: [HeaderComponent, FooterComponent, SidebarComponent],
  imports: [
    AppRoutingModule,
    HttpClientModule,
    SharedModule,
    BrowserAnimationsModule,
  ],
  exports:[
    AppRoutingModule,
    HeaderComponent,
    FooterComponent,
    SidebarComponent,
  ],
  providers: [
    { provide: 'BASE_CONFIG', useValue: { uri: 'http://localhost:3002' } },
    { provide: MAT_DATE_LOCALE, useValue: 'zh-CN' },
    // { provide: DateAdapter, useClass: MomentDateAdapter },
    // { provide: MAT_DATE_FORMATS, useValue: MD_FNS_DATE_FORMATS },
    // { provide: MatDatepickerIntl, useClass: DatepickerI18n },
    /**
     * The `RouterStateSnapshot` provided by the `Router` is a large complex structure.
     * A custom RouterStateSerializer is used to parse the `RouterStateSnapshot` provided
     * by `@ngrx/router-store` to include only the desired pieces of the snapshot.
     */
   // { provide: RouterStateSerializer, useClass: CustomRouterStateSerializer }
  ],
})
export class CoreModule {
  // 使用SkipSelf注解避免重复注入。去系统的父级找依赖。
  // 使用Optional注解 让SkipSelf作为可选，在第一次注入时候系统中并没有CoreModule时候成功注入。
  constructor(@Optional() @SkipSelf()  parent:CommonModule,
  ir:MatIconRegistry,
  ds:DomSanitizer){
    if(parent){
      throw new Error("核心模块已经加在！且只需要加载一次")
    }
    loadSvgResources(ir,ds);
  }
 }
 
